<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\Access;
use app\modules\admin\models\Role;
use app\modules\admin\models\RoleAccess;
use app\controllers\AdminController;
use app\modules\admin\models\User;

class RoleController extends AdminController
{
    /**
     * 角色列表
     */
    public function actionIndex()
    {
        $role_list = Role::find()->asArray()->all();
        Role::formatList($role_list);
        return $this->render('list', ['list' => $role_list]);
    }

    /**
     * 设置角色
     */
    public function actionSet()
    {
        $request = Yii::$app->request;
        if ($request->isGet) {
            $id = $this->get('id', '');
            $info = [];
            if ($id) {
                $info = Role::find()->where(['id' => $id])->one();
            }
            return $this->render('set', ['info' => $info]);
        }

        $id = $this->post('id', '');
        $role_name = $this->post('role_name', '');
        if (empty($role_name)) {
            $this->errorJson('角色不能为空');
        }

        $time = time();
        $roleInfo = Role::find()->where(['id' => $id])->one();
        if ($roleInfo) {
            $roleModel = $roleInfo;
        } else {
            $roleModel = new Role();
            $roleModel->status = 1;
            $roleModel->create_at = $time;
        }
        $roleModel->name = $role_name;
        $roleModel->update_at = $time;
        $result = $roleModel->save(0);
        $this->okJson();
    }

    /**
     * 删除角色
     */
    public function actionDel()
    {
        if (Yii::$app->request->isAjax) {
            $id = $this->post('id', '');
            if (!$id) {
                $this->errorJson('参数错误');
            }

            $info = Role::find()->where(['id' => $id])->one();
            if (!$info) {
                $this->errorJson('信息不存在');
            }
            $info->status = 0;
            $info->update_at = time();
            $info->save(0);
            $this->okJson();
        }
    }

    /**
     * 显示设置权限页面
     */
    public function actionShowSetting()
    {
        $roleInfo = $accessList = [];
        $roleId = $this->get('role_id', '');

        if ($roleId) {
            $roleInfo = Role::find()->where(['id' => $roleId])->asArray()->one();
        }

        // 已经关联的权限ID
        $selectAccessIds = RoleAccess::getSelectAccessIds($roleId);

        // 所有权限列表
        $accessList = Access::find()->where(['status' => 1])->all();

        $data = [
            'roleId' => $roleId,
            'roleInfo' => $roleInfo,
            'accessList' => $accessList,
            'selectAccessIds' => $selectAccessIds,
        ];
        return $this->render('setting', $data);
    }

    /**
     * 设置权限
     */
    public function actionSetting()
    {
         $role_id = $this->post('role_id', '');
         $access_ids = $this->post('access_ids', []);

         if (empty($role_id)) {
             $this->errorJson('参数错误');
         }

         $roleInfo = Role::find()->where(['id' => $role_id])->one();
         if (!$roleInfo) {
             $this->errorJson('角色信息不存在');
         }

        // 已经关联的权限ID
        $selectAccessIds = RoleAccess::getSelectAccessIds($role_id);

        // 要增加的
        $addAccessIds = array_diff($access_ids, $selectAccessIds);
        if ($addAccessIds) {
            foreach ($addAccessIds as $_accessId) {
                $model = new RoleAccess();
                $model->role_id = $role_id;
                $model->access_id = $_accessId;
                $model->create_at = time();
                $model->save(0);
            }
        }

        // 需要删除的权限
        $delAccessIds = array_diff($selectAccessIds, $access_ids);
        if ($delAccessIds) {
            RoleAccess::deleteAll(['role_id' => $role_id, 'access_id' => $delAccessIds]);
        }
        $this->okJson();
    }
}